<template>
	<view class="warp">
		<!-- #ifdef APP-PLUS -->
		<view class="title-contents"><view class="top-view status"></view></view>
		<!-- #endif -->
		<view class="login-title">
			<view class="logo"><image src="/static/images/common/logo.png" mode=""></image></view>
		</view>
		<view class="login-phone form-wapper">
			<view class="phone input-wapper">
				<view class="lside"><text class="iconfont icon-shouji"></text></view>
				<view class="mobile rside"><input placeholder-class="input-placeholder" v-model="username"  type="number" placeholder="请输入手机号" /></view>
			</view>
			<view class="code input-wapper">
				<view class="lside"><text class="iconfont icon-mima2"></text></view>
				<view class="rside"><input placeholder-class="input-placeholder" type="password" v-model="password" maxlength="12" placeholder="请输入密码(6-12位) " /></view>
			</view>
			<view class="paw"><view class="btn" @click="code">验证码登录</view></view>
		</view>

		<button type="primary" @click="login" class="login-btn">登录</button>
		<view class="reg-paw">
			<text class="active" @click="reg">注册账号</text>
			<text @click="forget">忘记密码</text>
		</view>
		<!-- #ifdef APP-PLUS -->
		<view class="quick">
			<view class="quick-title">
				<text class="line"></text>
				<text class="txt">快捷登陆</text>
				<text class="line"></text>
			</view>
			<view class="sanfang"><image src="../../static/temp/share_wechat.png" mode=""></image></view>
		</view>
		<!-- #endif -->
	</view>
</template>
<script>
	import {login as Login} from "@/api/user.js"
	export default{
		data(){
			return{
				username:'15896994682',
				password:'123456'
			}
		},
		methods:{
			reg() {
				uni.navigateTo({
					url: "/pages/public/reg"
				})
			},
			login(){
				let {username,password} = this
				if(!/^1[3-9]\d{9}$/.test(username)){
					uni.$u.toast('手机号码格式错误');
					// this.$toast('手机号码格式错误');
					return;
				}
				if(!/^\w{6,12}$/.test(password)){
					uni.$u.toast('密码格式错误');
					// this.$toast('密码格式错误');
					return;
				}
				Login({username,password}).then(res=>{
					// console.log(res)
					if (res.code==0) {
						uni.$u.toast("登录成功");
						uni.setStorageSync('storage_key', 'hello');
						uni.setStorageSync('token',res.token)
						uni.setStorageSync('tokenExpired',res.tokenExpired)
						uni.setStorageSync('uid',res.uid)
						let {token,tokenExpired,uid,username,userInfo} = res
						this.$store.commit('setData', {
							token,
							tokenExpired,
							uid,
							username,
							userInfo
						})
						setTimeout(()=>{
							uni.navigateBack()
						},500)
					}
				})
			},
			forget(){
				uni.navigateTo({
					url: "/pages/public/forget"
				})
			},
			code(){
				uni.navigateTo({
					url: "/pages/public/code_login"
				})
			}
		}
	} 
</script>
<style lang="scss">
@import  "login.scss"
</style>
